rm(list = setdiff(ls(), lsf.str()))

#UK - Understanding Society Data ---------------------
load("Study 1/Altered Data/Study1_UK_understanding.RData")

data_UK<-(with(data_UK, data.frame(total_populist, w3_agre , w3_open ,w3_con, w3_ext ,w3_neu ,female ,age , Ed_OtherQualification , Ed_GSCElevel, Ed_Alevel,Ed_higherdegree , Ed_degree, Ed_Missing, income ,  w6_cynicism)))

data_UK$education<-ifelse(data_UK$Ed_OtherQualification==1,1,0)
data_UK$education[data_UK$Ed_GSCElevel==1]=2
data_UK$education[data_UK$Ed_Alevel==1]=3
data_UK$education[data_UK$Ed_higherdegree==1]=4
data_UK$education[data_UK$Ed_degree==1]=5

data_UK<-na.omit(data_UK)
data_UK$id <- sample(21181, size = nrow(data_UK))
data_UK$id <- 1000000+data_UK$id 
data_UK$income<-zero1(data_UK$income)
data_UK <- data_UK[,which(colnames(data_UK)%in%c("id", "total_populist", "w3_agre", "w3_open", "w3_con","w3_ext","w3_neu","female", "age", "income", "education", "w6_cynicism"))]

data_UK$study <- 1
colnames(data_UK)[colnames(data_UK)=="total_populist"] <- "populist"
colnames(data_UK)[colnames(data_UK)=="w3_agre"] <- "agre"
colnames(data_UK)[colnames(data_UK)=="w3_open"] <- "open"
colnames(data_UK)[colnames(data_UK)=="w3_con"] <- "con"
colnames(data_UK)[colnames(data_UK)=="w3_ext"] <- "ext"
colnames(data_UK)[colnames(data_UK)=="w3_neu"] <- "neu"
colnames(data_UK)[colnames(data_UK)=="w6_cynicism"] <- "cynicism"

data_UK$agre<-zero1(data_UK$agre)
data_UK$open<-zero1(data_UK$open)
data_UK$con<-zero1(data_UK$con)
data_UK$ext<-zero1(data_UK$ext)
data_UK$neu<-zero1(data_UK$neu)
data_UK$cynicism<-zero1(data_UK$cynicism)
data_UK$education<-zero1(data_UK$education)
data_UK$income_missing<-0 
data_UK$social_cons<-2
data_UK$econ_cons<-2
data_UK$econ_cons_missing<-1
data_UK$social_cons_missing<-1
data_UK$cynicism_missing<-0
data_UK$lr_placement<-2
data_UK$lr_placement_missing<-1
data_UK$language<-0
data_UK$authoritarianism<-2
data_UK$authoritarianism_missing<-0

#UK - British Election Studies -----------------------
load("Study 1/Altered Data/Study1_UK_BES.RData")
data_BES$id <- sample(21811, size = nrow(data_BES))
data_BES$id <- 2000000+data_BES$id 
data_BES$education<-ifelse(data_BES$Ed_GSCE_DG==1,1,0)
data_BES$education[data_BES$Ed_GSCE_AC==1]=2
data_BES$education[data_BES$Ed_A_level==1]=3
data_BES$education[data_BES$Ed_Undergraduate==1]=4
data_BES$education[data_BES$Ed_Postgrad==1]=5

data_BES <- data_BES[,which(colnames(data_BES)%in%c("id", "w6_voteUKIP", "agre", "open", "con","ext","neu","female", "Age", "education", "income", "income_missing", "w6_pol_cynicism", "w4_immiatt", "w6_redistribution"))]
data_BES$study <- 2
colnames(data_BES)[colnames(data_BES)=="w6_voteUKIP"] <- "populist"
colnames(data_BES)[colnames(data_BES)=="sex"] <- "female"
colnames(data_BES)[colnames(data_BES)=="Age"] <- "age"
colnames(data_BES)[colnames(data_BES)=="w6_pol_cynicism"] <- "cynicism"
colnames(data_BES)[colnames(data_BES)=="w4_immiatt"] <- "social_cons"
colnames(data_BES)[colnames(data_BES)=="w6_redistribution"] <- "econ_cons"
data_BES$education<-zero1(data_BES$education)
data_BES$econ_cons_missing<-0
data_BES$social_cons_missing<-0
data_BES$cynicism_missing<-0
data_BES$lr_placement<-2
data_BES$lr_placement_missing<-1
data_BES$language<-0
data_BES$authoritarianism<-2
data_BES$authoritarianism_missing<-0

data_BES$agre<-zero1(data_BES$agre)
data_BES$open<-zero1(data_BES$open)
data_BES$con<-zero1(data_BES$con)
data_BES$ext<-zero1(data_BES$ext)
data_BES$neu<-zero1(data_BES$neu)
data_BES$cynicism<-zero1(data_BES$cynicism)
data_BES$social_cons<-zero1(data_BES$social_cons)
data_BES$econ_cons<-zero1(data_BES$econ_cons)

#Germany-----------
load("Study 1/Altered Data/Study1_Germany.RData")

data_germany <- data[,which(colnames(data)%in%c("z000001a", "vote_intention_populist", "agre", "open", "con", "ext","neu", "female", "age", "education", "income", "income_missing", "econ_cons", "social_cons"))]
colnames(data_germany)[colnames(data_germany)=="vote_intention_populist"] <- "populist"
colnames(data_germany)[colnames(data_germany)=="z000001a"] <- "id"

data_germany$open<- zero1(data_germany$open)
data_germany$con<- zero1(data_germany$con)
data_germany$ext<- zero1(data_germany$ext)
data_germany$agre<- zero1(data_germany$agre)
data_germany$neu<- zero1(data_germany$neu)
data_germany$social_cons<- zero1(data_germany$social_cons)

data_germany$econ_cons_missing<-0
data_germany$social_cons_missing<-0
data_germany$cynicism<-0
data_germany$cynicism_missing<-1
data_germany$language<-0
data_germany$authoritarianism<-2
data_germany$authoritarianism_missing<-0
data_germany$lr_placement<-2
data_germany$lr_placement_missing<-1
data_germany$study<-3

#Denmark 2010------------------------
load("Study 1/Altered Data/Study1_Denmark.RData")
data_DK <- data[,which(colnames(data)%in%c("vote_populist10_intention", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "v2010_education", "social", "econ_cons", "cynicism"))]
data_DK$study <- 4

data_DK$id <- sample(4090, size = nrow(data_DK))
data_DK$id <- 3000000+data_DK$id 
colnames(data_DK)[colnames(data_DK)=="vote_populist10_intention"] <- "populist"
colnames(data_DK)[colnames(data_DK)=="v2010_education"] <- "education"
colnames(data_DK)[colnames(data_DK)=="social"] <- "social_cons"

data_DK$open<- zero1(data_DK$open)
data_DK$con<- zero1(data_DK$con)
data_DK$ext<- zero1(data_DK$ext)
data_DK$agre<- zero1(data_DK$agre)
data_DK$neu<- zero1(data_DK$neu)
data_DK$education<- zero1(data_DK$education)
data_DK$econ_cons<- zero1(data_DK$econ_cons)
data_DK$cynicism<- zero1(data_DK$cynicism)
data_DK$social_cons<- zero1(data_DK$social_cons)
data_DK$econ_cons_missing<-0
data_DK$social_cons_missing<-0
data_DK$cynicism_missing<-0
data_DK$lr_placement<-2
data_DK$lr_placement_missing<-1
data_DK$language<-0
data_DK$authoritarianism<-2
data_DK$authoritarianism_missing<-0

#Denmark 2011------------------------
load("Study 1/Altered Data/Study1_Denmark.RData")
data_DK11 <- data[,which(colnames(data)%in%c("vote_populist11_choice", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "v2010_education", "social", "econ_cons", "cynicism"))]
data_DK11$study <- 5
data_DK11$id <- sample(4090, size = nrow(data_DK))
data_DK11$id <- 3000000+data_DK11$id
colnames(data_DK11)[colnames(data_DK11)=="vote_populist11_choice"] <- "populist"
colnames(data_DK11)[colnames(data_DK11)=="v2010_education"] <- "education"
colnames(data_DK11)[colnames(data_DK11)=="social"] <- "social_cons"

data_DK11$open<- zero1(data_DK11$open)
data_DK11$con<- zero1(data_DK11$con)
data_DK11$ext<- zero1(data_DK11$ext)
data_DK11$agre<- zero1(data_DK11$agre)
data_DK11$neu<- zero1(data_DK11$neu)
data_DK11$education<- zero1(data_DK11$education)
data_DK11$econ_cons<- zero1(data_DK11$econ_cons)
data_DK11$cynicism<- zero1(data_DK11$cynicism)
data_DK11$social_cons<- zero1(data_DK11$social_cons)
data_DK11$econ_cons_missing<-0
data_DK11$social_cons_missing<-0
data_DK11$cynicism_missing<-0
data_DK11$lr_placement<-2
data_DK11$lr_placement_missing<-1
data_DK11$language<-0
data_DK11$authoritarianism<-2
data_DK11$authoritarianism_missing<-0

#Netherlands Election 2012 -------------
load("Study 1/Altered Data/Study1_NL_12.RData")
data_NL12 <- data[,which(colnames(data)%in%c("nomem_encr", "populist", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "education", "cynicism11", "anti_immi11", "econ_cons11"))]
data_NL12$study <- 6
colnames(data_NL12)[colnames(data_NL12)=="nomem_encr"] <- "id"
colnames(data_NL12)[colnames(data_NL12)=="econ_cons11"] <- "econ_cons"
colnames(data_NL12)[colnames(data_NL12)=="anti_immi11"] <- "social_cons"
colnames(data_NL12)[colnames(data_NL12)=="cynicism11"] <- "cynicism"

data_NL12$econ_cons<- zero1(data_NL12$econ_cons)
data_NL12$social_cons<- zero1(data_NL12$social_cons)
data_NL12$cynicism<- zero1(data_NL12$cynicism)
data_NL12$education<- zero1(data_NL12$education)
data_NL12$open<- zero1(data_NL12$open)
data_NL12$con<- zero1(data_NL12$con)
data_NL12$ext<- zero1(data_NL12$ext)
data_NL12$agre<- zero1(data_NL12$agre)
data_NL12$neu<- zero1(data_NL12$neu)
data_NL12$econ_cons_missing<-0
data_NL12$social_cons_missing<-0
data_NL12$cynicism_missing<-0
data_NL12$lr_placement<-2
data_NL12$lr_placement_missing<-1
data_NL12$language<-0
data_NL12$authoritarianism<-2
data_NL12$authoritarianism_missing<-0

#Netherlands EU elections 2014----------------------
load("Study 1/Altered Data/Study1_NL_14.RData")

data_sub$education<-ifelse(data_sub$prep_secon==1,1,0)
data_sub$education[data_sub$highschool==1]=2
data_sub$education[data_sub$secondary_vocation==1]=3
data_sub$education[data_sub$pre_uni==1]=4
data_sub$education[data_sub$college==1]=5
data_sub$education[data_sub$uni==1]=6
NL_15 <- data_sub[,which(colnames(data_sub)%in%c("w1_pvv_national", "w5_agre", "w5_open", "w5_con","w5_extra","w5_neu","female", "age", "education", "income", "income_missing", "w1_cyn", "w1_immi"))]

NL_15$id <- sample(1174, size = nrow(NL_15))
NL_15$id <- 950000+NL_15$id 
colnames(NL_15)[colnames(NL_15)=="w1_pvv_national"] <- "populist"
colnames(NL_15)[colnames(NL_15)=="w5_agre"] <- "agre"
colnames(NL_15)[colnames(NL_15)=="w5_open"] <- "open"
colnames(NL_15)[colnames(NL_15)=="w5_con"] <- "con"
colnames(NL_15)[colnames(NL_15)=="w5_extra"] <- "ext"
colnames(NL_15)[colnames(NL_15)=="w5_neu"] <- "neu"
colnames(NL_15)[colnames(NL_15)=="sex"] <- "female"
colnames(NL_15)[colnames(NL_15)=="w1_immi"] <- "social_cons"
colnames(NL_15)[colnames(NL_15)=="w1_cyn"] <- "cynicism"

NL_15$open<- zero1(NL_15$open)
NL_15$con<- zero1(NL_15$con)
NL_15$ext<- zero1(NL_15$ext)
NL_15$agre<- zero1(NL_15$agre)
NL_15$neu<- zero1(NL_15$neu)
NL_15$education<-zero1(NL_15$education)
NL_15$social_cons<-zero1(NL_15$social_cons)
NL_15$cynicism<-zero1(NL_15$cynicism)
NL_15$study <- 7
NL_15$econ_cons <- 2
NL_15$econ_cons_missing <- 1
NL_15$social_cons_missing<-0
NL_15$cynicism_missing<-0
NL_15$lr_placement<-2
NL_15$lr_placement_missing<-1
NL_15$language<-0
NL_15$authoritarianism<-2
NL_15$authoritarianism_missing<-0

#Netherlands EU elections 2014 EU elections----------------------
load("Study 1/Altered Data/Study1_NL_14.RData")
data_sub$education<-ifelse(data_sub$prep_secon==1,1,0)
data_sub$education[data_sub$highschool==1]=2
data_sub$education[data_sub$secondary_vocation==1]=3
data_sub$education[data_sub$pre_uni==1]=4
data_sub$education[data_sub$college==1]=5
data_sub$education[data_sub$uni==1]=6

NL_15_vote <- data_sub[,which(colnames(data_sub)%in%c("w4_pvv_EU_vote", "w5_agre", "w5_open", "w5_con","w5_extra","w5_neu","female", "age", "education", "income", "income_missing", "w1_cyn", "w1_immi"))]
NL_15_vote$id <- sample(1174, size = nrow(NL_15_vote))
NL_15_vote$id <- 950000+NL_15_vote$id 
colnames(NL_15_vote)[colnames(NL_15_vote)=="w4_pvv_EU_vote"] <- "populist"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w5_agre"] <- "agre"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w5_open"] <- "open"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w5_con"] <- "con"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w5_extra"] <- "ext"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w5_neu"] <- "neu"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w1_immi"] <- "social_cons"
colnames(NL_15_vote)[colnames(NL_15_vote)=="w1_cyn"] <- "cynicism"

NL_15_vote$open<- zero1(NL_15_vote$open)
NL_15_vote$con<- zero1(NL_15_vote$con)
NL_15_vote$ext<- zero1(NL_15_vote$ext)
NL_15_vote$agre<- zero1(NL_15_vote$agre)
NL_15_vote$neu<- zero1(NL_15_vote$neu)
NL_15_vote$education<-zero1(NL_15_vote$education)
NL_15_vote$social_cons<-zero1(NL_15_vote$social_cons)
NL_15_vote$cynicism<-zero1(NL_15_vote$cynicism)
NL_15_vote$study <- 8
NL_15_vote$econ_cons <- 2
NL_15_vote$econ_cons_missing <- 1
NL_15_vote$social_cons_missing<-0
NL_15_vote$cynicism_missing<-0
NL_15_vote$lr_placement<-2
NL_15_vote$lr_placement_missing<-1
NL_15_vote$language<-0
NL_15_vote$authoritarianism<-2
NL_15_vote$authoritarianism_missing<-0

#Netherlands Election 2017 -------------
load("Study 1/Altered Data/Study1_NL_17.RData")
data_NL17 <- data[,which(colnames(data)%in%c("nomem_encr", "populist", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "education", "anti_immi", "econ_cons", "cynicism"))]

data_NL17$study <- 9
data_NL17$education<-zero1(data_NL17$education)
data_NL17$open<- zero1(data_NL17$open)
data_NL17$con<- zero1(data_NL17$con)
data_NL17$ext<- zero1(data_NL17$ext)
data_NL17$agre<- zero1(data_NL17$agre)
data_NL17$neu<- zero1(data_NL17$neu)
colnames(data_NL17)[colnames(data_NL17)=="nomem_encr"] <- "id"
colnames(data_NL17)[colnames(data_NL17)=="econ_cons"] <- "econ_cons"
colnames(data_NL17)[colnames(data_NL17)=="anti_immi"] <- "social_cons"
colnames(data_NL17)[colnames(data_NL17)=="cynicism"] <- "cynicism"
data_NL17$econ_cons<- zero1(data_NL17$econ_cons)
data_NL17$social_cons<- zero1(data_NL17$social_cons)
data_NL17$cynicism<- zero1(data_NL17$cynicism)

data_NL17$econ_cons_missing<-0
data_NL17$social_cons_missing<-0
data_NL17$cynicism_missing<-0
data_NL17$lr_placement<-2
data_NL17$lr_placement_missing<-1
data_NL17$language<-0
data_NL17$authoritarianism<-2
data_NL17$authoritarianism_missing<-0

#Switzerland Household Panel 2009 --------------------
load("Study 1/Altered Data/Study1_Swiss_Household09.RData")
data_swiss9 <- data[,which(colnames(data)%in%c("idint", "vote_svp", "agre", "open", "con","ext","neu","female", "age", "income", "income_mis", "education", "anti_immi", "econ_cons", "language"))]
colnames(data_swiss9)[colnames(data_swiss9)=="vote_svp"] <- "populist"
colnames(data_swiss9)[colnames(data_swiss9)=="idint"] <- "id"
colnames(data_swiss9)[colnames(data_swiss9)=="income_mis"] <- "income_missing"
colnames(data_swiss9)[colnames(data_swiss9)=="anti_immi"] <- "social_cons"
data_swiss9$id<-500000+data_swiss9$id
data_swiss9$study <- 10
data_swiss9$education <- zero1(data_swiss9$education)
data_swiss9$econ_cons_missing<-0
data_swiss9$social_cons_missing<-0
data_swiss9$cynicism<-2
data_swiss9$cynicism_missing<-1
data_swiss9$lr_placement<-2
data_swiss9$lr_placement_missing<-1
data_swiss9$authoritarianism<-2
data_swiss9$authoritarianism_missing<-0

#Switzerland Household Panel 2015-------------
load("Study 1/Altered Data/Study1_Swiss_Household15.RData")
data15$lr_placement[is.na(data15$lr_placement)]=2
data15$lr_placement_missing<-ifelse(data15$lr_placement==2,1,0)
data_swiss <- data15[,which(colnames(data15)%in%c("idint", "vote_svp15", "agre", "open", "con","ext","neu","female", "age", "education", "income_mis", "income", "lr_placement", "lr_placement_missing", "language"))]
data_swiss$study <- 11
colnames(data_swiss)[colnames(data_swiss)=="vote_svp15"] <- "populist"
colnames(data_swiss)[colnames(data_swiss)=="idint"] <- "id"
colnames(data_swiss)[colnames(data_swiss)=="income_mis"] <- "income_missing"
data_swiss$id<-500000+data_swiss$id
data_swiss$open<- zero1(data_swiss$open)
data_swiss$con<- zero1(data_swiss$con)
data_swiss$ext<- zero1(data_swiss$ext)
data_swiss$agre<- zero1(data_swiss$agre)
data_swiss$neu<- zero1(data_swiss$neu)
data_swiss$lr_placement<- zero1(data_swiss$lr_placement)
data_swiss$education <- zero1(data_swiss$education)
data_swiss$econ_cons_missing<-1
data_swiss$social_cons_missing<-1
data_swiss$cynicism_missing<-1
data_swiss$econ_cons<-2
data_swiss$social_cons<-2
data_swiss$cynicism<-2
data_swiss$authoritarianism<-2
data_swiss$authoritarianism_missing<-0

#Switzerland Election 2015-------------
load("Study 1/Altered Data/Study1_Swiss_Elections15.RData")
swiss_election <- data[,which(colnames(data)%in%c("vote_svp", "agre", "open", "con","ext","neu","female", "age", "education", "income_missing", "income", "econ_socialspending", "social_limit_immi","W3_spr"))]

swiss_election$study <- 12
swiss_election$id <- sample(11073, size = nrow(swiss_election))
swiss_election$id <- 1000000+swiss_election$id
colnames(swiss_election)[colnames(swiss_election)=="vote_svp"] <- "populist"
colnames(swiss_election)[colnames(swiss_election)=="social_limit_immi"] <- "social_cons"
colnames(swiss_election)[colnames(swiss_election)=="econ_socialspending"] <- "econ_cons"
colnames(swiss_election)[colnames(swiss_election)=="W3_spr"] <- "language"

swiss_election$open<- zero1(swiss_election$open)
swiss_election$con<- zero1(swiss_election$con)
swiss_election$ext<- zero1(swiss_election$ext)
swiss_election$agre<- zero1(swiss_election$agre)
swiss_election$neu<- zero1(swiss_election$neu)

swiss_election$education <- zero1(swiss_election$education)
swiss_election$social_cons <- zero1(swiss_election$social_cons)
swiss_election$econ_cons <- zero1(swiss_election$econ_cons)

swiss_election$econ_cons_missing<-0
swiss_election$social_cons_missing<-0
swiss_election$cynicism_missing<-1
swiss_election$cynicism<-2
swiss_election$econ_cons_missing<-0
swiss_election$social_cons_missing<-0
swiss_election$lr_placement<-2
swiss_election$lr_placement_missing<-1
swiss_election$authoritarianism<-2
swiss_election$authoritarianism_missing<-0

#Spain -----------------------
load("Study 1/Altered Data/Study1_Spain.Rdata")
data$lr_placement<-zero1(data$lr_placement)
data$lr_placement[is.na(data$lr_placement)]=2
data$lr_placement_missing<-ifelse(data$lr_placement==2,1,0)
data$agre <- zero1(data$A_critical)
data$id <- sample(6175, size = nrow(data))
data$id <- 700000+data$id 

data_spain <- data[,which(colnames(data)%in%c("id", "populist_vote", "agre", "open", "con","ext","neu","female", "age", "education", "lr_placement_missing", "lr_placement", "cynicism", "income", "income_missing"))]
data_spain$study <- 13
colnames(data_spain)[colnames(data_spain)=="populist_vote"] <- "populist"

data_spain$open<-zero1(data_spain$open)
data_spain$con<-zero1(data_spain$con)
data_spain$ext<-zero1(data_spain$ext)
data_spain$neu<-zero1(data_spain$neu)
data_spain$cynicism<-zero1(data_spain$cynicism)
data_spain$education<-zero1(data_spain$education)

data_spain$econ_cons_missing<-1
data_spain$social_cons_missing<-1
data_spain$cynicism_missing<-0
data_spain$econ_cons<-2
data_spain$social_cons<-2
data_spain$language<-0
data_spain$authoritarianism<-2
data_spain$authoritarianism_missing<-0

#Venezuela --------------------
load("Study 1/Altered Data/Study1_Venezuela07.RData")
data$id <- sample(1510, size = nrow(data))
data$id <- 600000+data$id 

data_venezuela <- data[,which(colnames(data)%in%c("id", "vote_populist", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "education_years", "lr_placement"))]
colnames(data_venezuela)[colnames(data_venezuela)=="vote_populist"] <- "populist"
colnames(data_venezuela)[colnames(data_venezuela)=="education_years"] <- "education"

data_venezuela$study <- 14
data_venezuela$open<- zero1(data_venezuela$open)
data_venezuela$con<- zero1(data_venezuela$con)
data_venezuela$ext<- zero1(data_venezuela$ext)
data_venezuela$agre<- zero1(data_venezuela$agre)
data_venezuela$neu<- zero1(data_venezuela$neu)
data_venezuela$education<- zero1(data_venezuela$education)
data_venezuela$lr_placement<- zero1(data_venezuela$lr_placement)

data_venezuela$econ_cons_missing<-1
data_venezuela$social_cons_missing<-1
data_venezuela$cynicism_missing<-1
data_venezuela$econ_cons<-2
data_venezuela$social_cons<-2
data_venezuela$cynicism<-2
data_venezuela$lr_placement_missing<-2
data_venezuela$language<-0
data_venezuela$authoritarianism<-2
data_venezuela$authoritarianism_missing<-0

#US ANES 2016-----------------------
load("Study 1/Altered Data/Study 1_US_ANES2016.Rdata")

data_anes <- data[,which(colnames(data)%in%c("vote_trump", "agreeableness", "authoritarianism", "openness", "conscientiousness","extraversion","neuroticism","female", "age", "education", "income", "income_missing", "post_lib_cons_placement"))]
data_anes$id <- sample(4271, size = nrow(data_anes))
data_anes$id <- 1100000+data_anes$id 

colnames(data_anes)[colnames(data_anes)=="vote_trump"] <- "populist"
colnames(data_anes)[colnames(data_anes)=="agreeableness"] <- "agre"
colnames(data_anes)[colnames(data_anes)=="openness"] <- "open"
colnames(data_anes)[colnames(data_anes)=="conscientiousness"] <- "con"
colnames(data_anes)[colnames(data_anes)=="extraversion"] <- "ext"
colnames(data_anes)[colnames(data_anes)=="neuroticism"] <- "neu"
colnames(data_anes)[colnames(data_anes)=="post_lib_cons_placement"] <- "lr_placement"

data_anes$education<- zero1(data_anes$education)
data_anes$lr_placement<- zero1(data_anes$lr_placement)

data_anes$open<- zero1(data_anes$open)
data_anes$con<- zero1(data_anes$con)
data_anes$ext<- zero1(data_anes$ext)
data_anes$agre<- zero1(data_anes$agre)
data_anes$neu<- zero1(data_anes$neu)
data_anes$authoritarianism<- zero1(data_anes$authoritarianism)

data_anes$econ_cons<-0
data_anes$econ_cons_missing<-1
data_anes$social_cons<-0
data_anes$social_cons_missing<-1
data_anes$cynicism<-0
data_anes$cynicism_missing<-1
data_anes$lr_placement_missing<-0
data_anes$language<-0
data_anes$authoritarianism_missing<-0
data_anes$study<-15

#Merge data---------------
data_meta <- rbind(data_NL17, NL_15, NL_15_vote, data_NL12, data_UK, data_BES, data_DK, data_DK11, data_swiss9, data_swiss, swiss_election,  data_spain, data_venezuela, data_anes, data_germany)
data_meta<-na.omit(data_meta)

# Other
#data_meta$language1<-ifelse(data_meta$language==1,1,0)
#data_meta$language2<-ifelse(data_meta$language==2,1,0)
#data_meta$language3<-ifelse(data_meta$language==3,1,0)

data_meta$study2<-ifelse(data_meta$study==2,1,0)
data_meta$study3<-ifelse(data_meta$study==3,1,0)
data_meta$study4<-ifelse(data_meta$study==4,1,0)
data_meta$study5<-ifelse(data_meta$study==5,1,0)
data_meta$study6<-ifelse(data_meta$study==6,1,0)
data_meta$study7<-ifelse(data_meta$study==7,1,0)
data_meta$study8<-ifelse(data_meta$study==8,1,0)
data_meta$study9<-ifelse(data_meta$study==9,1,0)
data_meta$study10<-ifelse(data_meta$study==10,1,0)
data_meta$study11<-ifelse(data_meta$study==11,1,0)
data_meta$study12<-ifelse(data_meta$study==12,1,0)
data_meta$study13<-ifelse(data_meta$study==13,1,0)
data_meta$study14<-ifelse(data_meta$study==14,1,0)
data_meta$study15<-ifelse(data_meta$study==15,1,0)

data_meta$study<-as.factor(data_meta$study)
data_meta$education<-as.numeric(data_meta$education)
data_meta$income<-as.numeric(data_meta$income)
data_meta$econ_cons<-as.numeric(data_meta$econ_cons)
data_meta$social_cons<-as.numeric(data_meta$social_cons)


save(data_meta, file="Study 1/Altered Data/Meta_study1.RData")

#full model --------------
model2 <- glmer(populist ~ agre + open + con + ext + neu +age + female + education + income + income_missing + social_cons  + econ_cons+ cynicism + lr_placement + study2 + study3 + study4 + study5+ study6 + study7 + study8 + study9 + study10 + study11 + study12 + study13 + study14 + study15+ (1|id) , data=data_meta, family="binomial", nAGQ=0) 
se <- sqrt(diag(vcov(model2)))
tab <- cbind(Est = fixef(model2), LL = fixef(model2) - 1.96 * se, UL = fixef(model2) + 1.96 * se)
tab<-as.data.frame(exp(tab))

#calculate Odds ratios
round(exp(cbind(OR=fixef(model2),confint(model2,parm="beta_",method="Wald"))),3)

#calculate 1 SD above and below the mean
low  <- mean(data_meta$agre, na.rm=T)-sd(data_meta$agre, na.rm=T)
high <- mean(data_meta$agre, na.rm=T)+sd(data_meta$agre, na.rm=T)

# Odds ratio
odds.difference <- (fixef(model2)[2]*low) / (fixef(model2)[2]*high)
1/odds.difference #1.67 more likely to vote for a populist

favorability.tables1 = stargazer(model2, title="Meta-analysis: samples from UK, Germany, Denmark, Netherlands, Switzerland, Spain, Venezela and U.S.", align=TRUE, covariate.labels = c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Age", "Female", "Education", "Income", "Income missing","Social conservatism", "Economic conservatism", "Cynicism", "L-R placement", "Sample: UK Election", "Sample: Germany", "Sample: Denmark 2010", "Sample: Denmark: 2011", "Sample: NL 2012", "Sample: NL 2014", "Sample: NL 2015", "Sample: NL 2017", "Sample: Swiss Household 09", "Sample: Swiss Household 15", "Sample: Swiss Elections", "Sample: Spain", "Sample: Venezuela", "Sample: US", "Intercept") , star.cutoffs=c(0.05), dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",  notes = "*p<0.05", label = "tab:Meta_odds",  notes.append = FALSE, no.space=TRUE)
cat(favorability.tables1, sep = '\n',  file = "Tables/Meta_all.tex")

#Predicted probability for Agreeableness: 1 sd below and above the mean
#agre_list <- list(agre = c(low, high))       # the example values
#ef.agre=effect("agre", model2, xlevels = agre_list)
#df.agre=data.frame(ef.agre)

#calculate 2 SD above and below the mean
#low2sd  <- mean(data_meta$agre, na.rm=T)-(sd(data_meta$agre, na.rm=T)*2)
#high2sd <- 1
#agre_list2 <- list(agre = c(low2sd, high2sd))       # the example values
#ef.agre2sd=effect("agre", model2, xlevels = agre_list2)
#df.agre2s=data.frame(ef.agre2sd)

#Predicted probability for woman

#female_list <- list(female = c(0, 1))       # the example values
#ef.female=effect("female", model2, xlevels = female_list)
#df.female=data.frame(ef.female)

